import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('../views/Login')
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/index',
    component: () => import('../views/Index'),
    children: [
      {
        path: 'admin',
        component: () => import('../views/Admin')
      },
      {
        path: 'teacher',
        component: () => import('../views/Teacher')
      },
      {
        path: 'student',
        component: () => import('../views/Student')
      },
      {
        path: 'subject',
        component: () => import('../views/Subject')
      },
      {
        path: 'question',
        component: () => import('../views/Question')
      },
      {
        path: 'grade',
        component: () => import('../views/GradeClass')
      },
      {
        path: 'paper',
        component: () => import('../views/TestPaper')
      },
      {
        path: 'exam',
        component: () => import('../views/StudentTestPaper')
      },
      {
        path: 'score',
        component: () => import('../views/ScoreLog')
      },
      {
        path: 'examLog',
        component: () => import('../views/StudentScoreLog')
      }
    ]
  },
  {
    path: '/examination/:id',
    component: () => import('../views/Examination')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  if (!sessionStorage.getItem('token')) return next('/login')
  next()
});

export default router
